package com.tensquare.notice.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.entity.Result;
import com.tensquare.notice.client.ArticleClient;
import com.tensquare.notice.client.UserClient;
import com.tensquare.notice.dao.NoticeDao;
import com.tensquare.notice.dao.NoticeFreshDao;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Autowired
    private NoticeFreshDao noticeFreshDao;

    @Autowired
    private ArticleClient articleClient;

    @Autowired
    private UserClient userClient;

    private void getInfo(Notice notice){
        Result userClientById = userClient.findById(notice.getOperatorId());
        HashMap data1 = (HashMap) userClientById.getData();
        notice.setOperatorName(data1.get("nickname").toString());

        Result TargetId = articleClient.getById(notice.getTargetId());
        HashMap data2 = (HashMap) TargetId.getData();
        System.err.println("******"+data2);
        notice.setTargetName(data2.get("title").toString());
    }

    public Notice findById(String id){
        Notice notice = noticeDao.selectById(id);
        getInfo(notice);
        return notice;
    }

    public Page<Notice> findPage(Integer page, Integer size, Notice notice) {
        Page<Notice> pageData = new Page<>(page,size);
        List<Notice> noticeList = noticeDao.selectPage(pageData, new EntityWrapper<>(notice));
        /*for (Notice n:noticeList){
            getInfo(n);
        }*/
        pageData.setRecords(noticeList);
        return pageData;

    }

    public void add(Notice notice) {
        noticeDao.insert(notice);
    }

    public void update(Notice notice) {
        noticeDao.updateById(notice);
    }

    public Page<NoticeFresh> freshPage(String userId, Integer page, Integer size) {
        NoticeFresh noticeFresh = new NoticeFresh();
        noticeFresh.setUserId(userId);

        Page<NoticeFresh> pageData = new Page<>(page,size);
        List<NoticeFresh> noticeFreshList = noticeFreshDao.selectPage(pageData, new EntityWrapper<>(noticeFresh));
        pageData.setRecords(noticeFreshList);
        return pageData;
    }

    public void freshDelete(NoticeFresh noticeFresh) {
        noticeFreshDao.delete(new EntityWrapper<>(noticeFresh));
    }
}
